#include "main.h"
#include <pd_timer.h>
#include <pd_timer_etr.h>
#include <hd_led.h>
#include <hd_oled.h>
#include <delay.h>
#include <hd_oled_protocol_spi.h>
#include <hd_oled_protocol_i2c.h>
#include <stddef.h>
#include <pd_i2c_hd.h>
#include <hd_flash.h>

#define OLED_PROTOCOL       2   // Select a protocol. SPI, 1; I2C, 2;
#define OLED_PROTOCOL_IMPL  2   // Select a implement. simulate, 1; hardware, 2;

LED *led;
uint16_t num;
static OLED *oled;

void oled_instance();

extern void SystemClock_Config(void);

int main(void) {
    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
    LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);

    SystemClock_Config();

    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);

    GPIO *ledGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_2);
    led = LEDBuilder.build(ledGPIO);
    led->init(led);

    oled_instance();

    oled->showString(oled, 0, 0, "TIM External CLK");
    oled->showString(oled, 0, 16, "Num:");
    oled->showString(oled, 0, 32, "Cnt:");
    oled->refresh(oled);

    // Set PD2's AF2 as TIM3's external clock source(TIM3_ETR)
    GPIO *timerGPIO = GPIOBuilder.build(GPIOD, LL_GPIO_PIN_2);
    timerGPIO->Mode = LL_GPIO_MODE_ALTERNATE;
    timerGPIO->Alternate = LL_GPIO_AF_2;
    TimerCh *timerCh = TimerChBuilder.build(timerGPIO, 1, NULL, 0);

    Timer *timer = TimerBuilder.build(TIM3, 1 - 1, 10 - 1);

    TimerETR *timerETR = TimerETRBuilder.build(timer, timerCh);
    timerETR->init(timerETR, TIM3_IRQn, 2, 2);

    while (1) {
        oled->showNum(oled, 32, 16, num, 6);
        oled->showNum(oled, 32, 32, timer->get_counter(timer), 6);
        oled->refresh(oled);

        delay_ms(1);
    }

}

void TIM3_IRQHandler(void) {
    if (LL_TIM_IsActiveFlag_UPDATE(TIM3)) {
        LL_TIM_ClearFlag_UPDATE(TIM3);

        num++;

        led->turn(led);
    }
}

void oled_instance() {
#if OLED_PROTOCOL == 1
    GPIO *sckGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_5); // Clock, SPIx_SCK
    GPIO *sdaGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_7); // MOSI,  SPIx_MOSI
    GPIO *resGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_3); // Reset, General IO, the chip reset pin is better
    GPIO *dcGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_1);  // Data or Command, General IO
    GPIO *csGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_2);  // Chip Select(Slave Select), General IO
    SPIGPIO *spiGPIO = SPIGPIOBuilder.build(&csGPIO, 1, sckGPIO, sdaGPIO, NULL);

#if OLED_PROTOCOL_IMPL == 1
    SPISimulate *spiObj = SPISimulateBuilder.build(spiGPIO);
#elif OLED_PROTOCOL_IMPL == 2
    SPIHardware *spiObj = SPIHardwareBuilder.build(SPI1, spiGPIO);
#endif
    SPI *spi1 = (SPI *) spiObj;
    spi1->init(spi1);
    OLEDProtocolSPI *oledProtocolObj = OLEDProtocolSPIBuilder.build(spi1, resGPIO, dcGPIO, csGPIO);

#elif OLED_PROTOCOL == 2
    GPIO *sckGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_10);  // I2Cx_SCL
    GPIO *sdaGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_11);  // I2Cx_SDA
#if OLED_PROTOCOL_IMPL == 1
    I2CSimulate *i2CObj = I2CSimulateBuilder.build(sckGPIO, sdaGPIO);
#elif OLED_PROTOCOL_IMPL == 2
    I2CHardware *i2CObj = I2CHardwareBuilder.build(I2C2, sckGPIO, sdaGPIO);
#endif
    I2C *i2c = (I2C *) i2CObj;
    i2c->init(i2c);
    OLEDProtocolI2C *oledProtocolObj = OLEDProtocolI2CBuilder.build(i2c);
#endif

    StorageFlash *storageFlash = StorageFlashBuilder.build();
    OLEDFont *oledFont = OLEDFontBuilder.build((Storage *) storageFlash, ENCODING_UTF8);
    oled = OLEDBuilder.build((OLEDProtocol *) oledProtocolObj, oledFont);
    oled->init(oled);
}


